<template>
  <div>
    <template>
      <Card style="width:850px;margin: auto;">
        <Form :model="formLeft" label-position="left" :rules="validateLibForm" :label-width="150"
              style="width: 300px;margin: auto;">

          <FormItem label="物料类型名称:" prop="typeName">
            <Input v-model="formLeft.typeName" :maxlength="10"></Input>
          </FormItem>

          <FormItem label="类型:" prop="type">
            <Select v-model="formLeft.type">
              <Option v-for="item in typeList" :value="item.label" :key="item.label">{{ item.value }}
              </Option>
            </Select>
          </FormItem>

          <FormItem label="描述:" prop="describe">
            <Input v-model="formLeft.describe" type="textarea"></Input>
          </FormItem>

          <row type="flex" justify="center">
            <i-col span="20">

            </i-col>
            <i-col span="30">
              <Button type="primary" @click="MaterialTypeInsert">添加</Button>
              <Button type="success" style="margin-left: 160px" @click="back">返回</Button>
            </i-col>
          </row>

        </Form>
      </Card>
    </template>
  </div>
</template>

<script>
  import {MaterialTypeInsert} from "../../../api/data"

  export default {
    data() {
      return {
        typeList: [
          {
            value: '原材料',
            label: '原材料'
          }, {
            value: '半成品',
            label: '半成品'
          }
        ],
        formLeft: {
          typeName: '',
          type:'',
          describe: '',
        },
        validateLibForm: {
          typeName: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          describe: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
        }
      }
    },
    mounted() {

    },
    methods: {
      Alphanumeric(e) {
        e.target.value = e.target.value.replace(/[^\w]/g,"");
      },
      MaterialTypeInsert() {
            if (this.formLeft.typeName !== null && this.formLeft.typeName !== "") {
              if (this.formLeft.type !== null && this.formLeft.type !== "") {
              MaterialTypeInsert(this.formLeft).then(res => {
                if (res.data.code == 0) {
                  this.$Message.success("编辑成功");
                  this.$router.go(-1);
                } else {
                  this.$Message.error(res.data.msg);
                }
              })
              } else {
                this.$Message.error("类型不能为空");
              }
          } else {
            this.$Message.error("物料类型名称不能为空");
          }
      },
      back() {
        this.$router.go(-1);
      },

    }
  }
</script>


